package com.global.college.views.dialog;


import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.global.college.R;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class UpdateDialog {
    private Context context;
    private Dialog dialog;
    private RelativeLayout lLayout_bg;
    private TextView tvContent;
    private ProgressBar progressBar;
    private TextView tv_progress;
    private TextView tv_downloadingWait;
    private TextView tvConfirm;
    private ImageView iv_background;


    public UpdateDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
    }

    public UpdateDialog builder() {

        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_update, null);
        // 获取自定义Dialog布局中的控件
        lLayout_bg = view.findViewById(R.id.lLayout_bg);
        tvContent = view.findViewById(R.id.tv_contentmsg);
        progressBar = view.findViewById(R.id.progressBar);
        iv_background = view.findViewById(R.id.iv_background);
        tv_progress = view.findViewById(R.id.tv_progress);
        tv_downloadingWait = view.findViewById(R.id.tv_downloadingWait);
        tvConfirm = view.findViewById(R.id.tv_confirm);


        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);
        return this;
    }

    public UpdateDialog setContent(String msg) {
        tvContent.setText(msg);
        return this;
    }

    //点击消失
    public UpdateDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    //点击外部消失
    public UpdateDialog setCanceledOnTouchOutside(boolean cancel) {
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }


    // 关闭loading
    public void removeLoading() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    /**
     * 下载中
     */
    public void download() {

        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                emitter.onNext("");
                emitter.onComplete();
            }
        });
        DisposableObserver<String> disposableObserver = new DisposableObserver<String>() {
            @Override
            public void onNext(String result) {
                tvConfirm.setVisibility(View.GONE);
                tvContent.setText(R.string.please_update_immediately);
                tv_progress.setVisibility(View.VISIBLE);
                tv_downloadingWait.setVisibility(View.VISIBLE);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(disposableObserver);
    }

    /**
     * 网络连接异常
     */
    public void networkError() {
        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                emitter.onNext("");
                emitter.onComplete();
            }
        });
        DisposableObserver<String> disposableObserver = new DisposableObserver<String>() {
            @Override
            public void onNext(String result) {
                tvConfirm.setVisibility(View.VISIBLE);
                tvConfirm.setText(R.string.retry);
                tvContent.setText(R.string.please_check_your_network_connection_and_try_again);
                tv_progress.setVisibility(View.GONE);
                tv_downloadingWait.setVisibility(View.GONE);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(disposableObserver);
    }

    public void downloadFailure() {
        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                emitter.onNext("");
                emitter.onComplete();
            }
        });
        DisposableObserver<String> disposableObserver = new DisposableObserver<String>() {
            @Override
            public void onNext(String result) {

                tvConfirm.setText(R.string.retry);
                tvConfirm.setVisibility(View.VISIBLE);
                tvContent.setText(R.string.download_failure);
                tv_progress.setVisibility(View.GONE);
                tv_downloadingWait.setVisibility(View.GONE);

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(disposableObserver);
    }

    //确认
    public UpdateDialog setConfirm(String text, final View.OnClickListener listener) {
        if ("".equals(text)) {
            tvConfirm.setText(R.string.update_immediately);
        } else {
            tvConfirm.setText(text);
        }

        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);

            }
        });
        return this;
    }


    public void show() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void setProgressBar(int progress) {
        progressBar.setProgress(progress);
        tv_progress.setText(progress + "%");
    }
}